package javacode.secondary_class;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
 * Lop nay khong co cac thuoc tinh, chi co cac phuong thuc dac trung danh rieng
 * cho viec cat cac chuoi dac biet
 */
public class ConvertString {

    /**
     * cat chuoi lay phan ten command
     */
    public static String getCommand(String x) {
        String rs = "";
        int number = 0;
        char[] ar = x.toCharArray();
        for (int i = 0; i < ar.length; i++) {
            if (":".equals(String.valueOf(ar[i]))) {
                number = i;
                rs = x.substring(0, number);
            }
        }
        String command_rq = "";
        boolean same = false;
        try {
            FileInputStream excel = new FileInputStream("lib\\Command_list.xls");// tao luong doc file danh sach cac lenh can lay
            HSSFWorkbook command_list = new HSSFWorkbook(excel);
            HSSFSheet sheet = command_list.getSheetAt(0);
            int num_row = sheet.getPhysicalNumberOfRows();

            for (int i = 1; i <= num_row; i++) {
                command_rq = sheet.getRow(i).getCell(0).getStringCellValue();
                //System.out.println(rs+"^"+command_rq);
                if (command_rq.equals(rs))//{
                {
                    same = true;
                }
                //System.out.println(same);}
            }
            excel.close();
        } catch (IOException ex) {
            Logger.getLogger(ConvertString.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "Lỗi đọc file Commandlist");
        }
        if (!same) {
            rs = null;                                                         
        }
        //System.out.println(same);
        return rs;
    }

    public static String getDescription(String x) {                             // cat chuoi bo phan dau lay doan description
        int number = x.indexOf(" ");
        return x.substring(number + 1, x.length());
    }
    
    public static String getParamaterRQ(String full, String need) {
        String rs = new String();
        String tmp_rs = new String();
        String tmp_full = full;
        String tmp_need = need;
        int index_p = 0;
        int index_command = 0;
        if (need.length() > 0) {
            while (tmp_need.length() > 1) {
                index_p = tmp_need.indexOf(":");
                String tmp = tmp_need.substring(0, index_p + 1);
                tmp_need = tmp_need.substring(index_p + 1, tmp_need.length());
                index_command = tmp_full.indexOf(tmp);
                tmp_full = tmp_full.substring(index_command, tmp_full.length());
                index_p = tmp_full.indexOf(";");
                rs += tmp_full.substring(0, index_p);
                tmp_full = tmp_full.substring(index_p, tmp_full.length());
            }
        }
        return rs;

    }
    public static String getInputFormat(String command, String list_input) {
        String rs = "";
            rs = list_input.substring(list_input.indexOf(command), list_input.length());
            rs = rs.substring(0, rs.indexOf(";"));
        return rs;
    }
    public static boolean retrieval(String x) {
        boolean kq = false;
        x = x.substring(0, 4);
        if (x.endsWith("RTRV")) {
            kq = true;
        }
        return kq;
    }
    
    public static String getRetrieval(String x) {
        String rs = null;
        int number = 0;
        int tmp = 0;
        char[] ar = x.toCharArray();
        while (tmp == 0) {

            if (":".equals(String.valueOf(ar[number])) || " ".equals(String.valueOf(ar[number]))) {
                //  System.out.println(ar[number]);
                tmp = 1;
                rs = x.substring(0, number);
                //  System.out.println(rs+"nhan");       

            }
            number++;
        }
        // System.out.println(rs);
        return rs;
    }

    public static String getDefault(String x) {
        String rs = "";
        int element = x.indexOf("The factory default is");
        if( element >= 0){
            x = x.substring(element, x.length());
            x = x.substring(x.indexOf((char)34)+1, x.lastIndexOf((char)34));
            if(x.equals("NULL"))
                rs = "NONE";
            else
                rs = x;
        }else{ 
            rs = "Null";
        }
        return rs;
    }
    public static String getEntry(String x) {
        String rs = null;
        int number = x.indexOf(" ");
        rs = x.substring(number + 1, x.length());


        return rs;
    }

    public static String getEntry2(String x) {
        String rs = null;
        int number = x.indexOf(" ");
        rs = x.substring(0, number + 1);
        return rs;
    }

    public static int getEntry3(String x) {
        int rs = 0;
        char ar[] = x.toCharArray();
        if ("&".equals(String.valueOf(ar[0]))) {
            rs = 1;
        }
        return rs;
    }
    /** ham lay dia chi thu muc cha cua file tu doan String */
    public static String getPath(String x) {
       int number = x.lastIndexOf((char)92);
       return   x.substring(0, number + 1);
    }
    
    /** ham lay ten file bo phan mo rong*/
    public static String getFileName(String filename){
       int number = filename.indexOf(".");
        return filename.substring(0, number);       
    }
    
    /** ham lay ten file tu string path, lay ca phan mo rong */
    public static String getFileNameFull(String path){
       int number = path.lastIndexOf((char)92);
        return path.substring(number+1, path.length());       
    }
    
    /** ham check su hop le cua duong dan file excel xuat ra lay tu giao dien */
    public static boolean checkPath(String xml_path, String excel_path){
        try{
        File in_xml = new File(xml_path);
        //System.out.println(excel_path);
        String dir_excelout = getPath(excel_path);                              // chua duong dan toi thu muc chua file excel xuat ra
        //System.out.println(dir_excelout);
        String type_excelout= excel_path.substring(excel_path.lastIndexOf("."), excel_path.length()); // lay type file nguoi dung nhap vao
        //System.out.println(type_excelout);
        File out_excel =new File(dir_excelout);
        if(!in_xml.exists() || xml_path.equals("")){
            JOptionPane.showMessageDialog(null, "Vui lòng kiểm tra lại đường dẫn file XML");
            return  false;
        }else if(!out_excel.exists()){
            JOptionPane.showMessageDialog(null, "Vui lòng kiểm tra lại đường dẫn lưu file excel");
            return false;
        }else if(!type_excelout.equals(".xls")){
            JOptionPane.showMessageDialog(null, "Vui lòng kiểm tra lại kiểu file xuất ra, chỉ chấp nhận .XLS");
            return false;
        }else
            return true;
        }catch (Exception ex){
            return false;
        } 
    }
    
    
  /*  public static void main(String args[]){
      String a = "D://NienLuan//tl1guide.xml";
      //String b = (char)92;
      a= a.replace("//", String.valueOf((char)92));
      //File aa = new File(a);
      System.out.println(a);
    }*/
}